<?php

/**
 * This is the model class for table "gn_members".
 *
 * The followings are the available columns in table 'gn_members':
 * @property integer $member_id
 * @property string $salute
 * @property string $first_name
 * @property string $last_name
 * @property string $middle_name
 * @property string $email
 * @property string $mobile_no
 * @property string $birthday
 * @property string $tin
 * @property string $acr_no
 * @property string $nationality
 * @property string $main_address
 * @property integer $province_id
 * @property string $postal_code
 * @property string $secondary_address
 * @property string $business_name
 * @property string $business_type
 * @property string $business_address
 * @property string $business_tin
 * @property string $position
 * @property integer $auto_renew
 * @property integer $auto_order
 * @property string $level
 * @property integer $sponsor_id
 * @property string $date_joined
 * @property integer $status_id
 * @property string $status_date
 * @property string $status_remark
 * @property string $date_created
 * @property string $bank_name
 * @property string $bank_account_no
 * @property string $bank_account_type
 * @property string $credit_card_name
 * @property string $credit_card_account_no
 * @property string $credit_card_type
 * @property integer $last_modified_by
 */
class Members extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Members the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'gn_members';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('salute, first_name, last_name, mobile_no, birthday, nationality, main_address, province_id, date_joined, status_id, status_date', 'required'),
			array('province_id, auto_renew, auto_order, sponsor_id, status_id, last_modified_by', 'numerical', 'integerOnly'=>true),
			array('salute', 'length', 'max'=>2),
			array('first_name, last_name, middle_name', 'length', 'max'=>32),
			array('email, mobile_no, tin, business_name', 'length', 'max'=>45),
			array('acr_no, nationality, business_type', 'length', 'max'=>64),
			array('main_address, secondary_address, business_address', 'length', 'max'=>90),
			array('postal_code', 'length', 'max'=>10),
			array('business_tin', 'length', 'max'=>24),
			array('position', 'length', 'max'=>5),
			array('level', 'length', 'max'=>8),
			array('status_remark', 'length', 'max'=>256),
		    array('bank_name, bank_account_no, bank_account_type, credit_card_name, credit_card_account_no, credit_card_type', 'length', 'max'=>128),
			//array('email', 'email','checkMX'=>true),
			array('position', 'validatePosition'),
			array('birthday', 'validateAge', 'min_age'=>18 ),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('member_id, salute, first_name, last_name, middle_name, email, mobile_no, birthday, tin, acr_no, nationality, main_address, province_id, postal_code, secondary_address, business_name, business_type, business_address, business_tin, position, auto_renew, auto_order, level, sponsor_id, date_joined, status_id, status_date, status_remark, date_created, bank_name, bank_account_no, bank_account_type, credit_card_name, credit_card_account_no, credit_card_type, last_modified_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		
		return array(
		   'Sales'=>array(self::HAS_MANY, 'Sales', 'member_id'),
		   'Payouts'=>array(self::HAS_MANY, 'Payouts', 'member_id'),
		);
		 
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'member_id' => 'Member',
			'salute' => 'Salute',
			'first_name' => 'First Name',
			'last_name' => 'Last Name',
			'middle_name' => 'Middle Name',
			'email' => 'Email',
			'mobile_no' => 'Mobile No',
			'birthday' => 'Birthday',
			'tin' => 'Tin',
			'acr_no' => 'Acr No',
			'nationality' => 'Nationality',
			'main_address' => 'Main Address',
			'province_id' => 'Province',
			'postal_code' => 'Postal Code',
			'secondary_address' => 'Secondary Address',
			'business_name' => 'Business Name',
			'business_type' => 'Business Type',
			'business_address' => 'Business Address',
			'business_tin' => 'Business Tin',
			'position' => 'Position',
			'auto_renew' => 'Auto Renew',
			'auto_order' => 'Auto Order',
			'level' => 'Level',
			'sponsor_id' => 'Sponsor',
			'date_joined' => 'Date Joined',
			'status_id' => 'Status',
			'status_date' => 'Status Date',
			'status_remark' => 'Status Remark',
			'date_created' => 'Date Created',
			'bank_name' => 'Bank Name',
			'bank_account_no' => 'Bank Account No',
			'bank_account_type' => 'Bank Account Type',
			'credit_card_name' => 'Credit Card Name',
			'credit_card_account_no' => 'Credit Card Account No',
			'credit_card_type' => 'Credit Card Type',
			'last_modified_by' => 'Last Modified By',
			'first_name' => 'First Name',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('member_id',$this->member_id);
		$criteria->compare('salute',$this->salute,true);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('middle_name',$this->middle_name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile_no',$this->mobile_no,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('tin',$this->tin,true);
		$criteria->compare('acr_no',$this->acr_no,true);
		$criteria->compare('nationality',$this->nationality,true);
		$criteria->compare('main_address',$this->main_address,true);
		$criteria->compare('province_id',$this->province_id);
		$criteria->compare('postal_code',$this->postal_code,true);
		$criteria->compare('secondary_address',$this->secondary_address,true);
		$criteria->compare('business_name',$this->business_name,true);
		$criteria->compare('business_type',$this->business_type,true);
		$criteria->compare('business_address',$this->business_address,true);
		$criteria->compare('business_tin',$this->business_tin,true);
		$criteria->compare('position',$this->position,true);
		$criteria->compare('auto_renew',$this->auto_renew);
		$criteria->compare('auto_order',$this->auto_order);
		$criteria->compare('level',$this->level,true);
		$criteria->compare('sponsor_id',$this->sponsor_id);
		$criteria->compare('date_joined',$this->date_joined,true);
		$criteria->compare('status_id',$this->status_id);
		$criteria->compare('status_date',$this->status_date,true);
		$criteria->compare('status_remark',$this->status_remark,true);
		$criteria->compare('date_created',$this->date_created,true);
		$criteria->compare('bank_name',$this->bank_name,true);
		$criteria->compare('bank_account_no',$this->bank_account_no,true);
		$criteria->compare('bank_account_type',$this->bank_account_type,true);
		$criteria->compare('credit_card_name',$this->credit_card_name,true);
		$criteria->compare('credit_card_account_no',$this->credit_card_account_no,true);
		$criteria->compare('credit_card_type',$this->credit_card_type,true);
		$criteria->compare('last_modified_by',$this->last_modified_by);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function validateAge( $attribute, $params )
	{
		$date_now = strtotime(date("Y-m-d") );
		$date_birth = strtotime( $this->$attribute );
	    $days = floor(($date_now-$date_birth)/(60*60*24));
		$age  = floor($days/365);
	
		if ( $age < $params['min_age'] )
		{
	      $this->addError($attribute, "Member should be at least 18 years old.");
		}
	}
	
	public function validatePosition( $attribute, $params )
	{
		if ( $this->isNewRecord )
		{
			$connection = Yii::app()->db;

			#Get Sponsor List   
			$sql = "SELECT member_id,  first_name, last_name 
			          FROM gn_members 
					 WHERE sponsor_id=" . $this->sponsor_id . "
					   AND position='".$this->$attribute."'";
					 
			$command = $connection->createCommand($sql);
			$dataReader = $command->query();
			
			if ( $data = $dataReader->read() )
			  $this->addError($attribute, "Sponsor[".$this->sponsor_id."] already have a " . $this->$attribute . " member.");
			
		}
	}
}